﻿using System.Collections.Generic;
using System.Linq;
using xTeam.Glgcx.CommonUtilities;
using System;
using System.Management;
using System.Data.Entity.Infrastructure;


namespace xTeam.Glgcx.DataAccess
{
    public class ArchiveDataAccess
    {
        private GlgcxDataBase _dataBase;
        public ArchiveDataAccess()
        {
            _dataBase = new GlgcxDataBase();
        }
        #region 文字
        public ENews GetArchiveById(int id)
        {

            var article = _dataBase.Archive.FirstOrDefault(i => i.NewsId == id);
            if (article != null)
            {
                article.Album = _dataBase.Album
                    .Where(i => i.NewsId == article.NewsId)
                    .OrderByDescending(i => i.IsCover)
                    .ThenBy(i => i.Sequence)
                    .ToList();
            }

            return article;

        }

        public List<ENews> ArchiveList()
        {


          var query = _dataBase.Archive.OrderBy(i => i.NewsId).Skip(10).Take(5);

          DbQuery<ENews> dbQuery = query as DbQuery<ENews>;
 

          return query.ToList();
 

            return _dataBase.Archive.AsEnumerable().GroupJoin(_dataBase.Album.AsEnumerable()
                , a => a.NewsId, b => b.NewsId
                , (a, b) =>
                {
                    a.Album = b.OrderByDescending(i => i.IsCover).ThenBy(i => i.Sequence).ToList();
                    return a;
                }).Join(_dataBase.Classify.AsEnumerable(), a => a.ClassifyId, b => b.ClassifyId
                    , (a, b) =>
                    {
                        a.Classify = b;
                        return a;
                    })
                    .OrderByDescending(i => i.AddDateTime)
                    .ToList();

        }

        public int Delete(int newsId)
        {
            int result = 0;

            var news = this.GetArchiveById(newsId);
            if (news != null)
            {
                _dataBase.Archive.Remove(news);
                _dataBase.SaveChanges();
            }

            return result;
        }

        public int Update(ENews entity)
        {

            int result = 0;
            var news = this.GetArchiveById(entity.NewsId);
            bool isNew = news == null;
            if (isNew)
                news = new ENews();

            news.AddDateTime = entity.AddDateTime;
            news.Author = entity.Author;
            news.ClassifyId = entity.ClassifyId;
            news.ClickCount = entity.ClickCount;
            news.Contents = entity.Contents;
            news.Description = entity.Description;
            news.ETag = entity.ETag;
            news.Isvalid = entity.Isvalid;
            news.Keywords = entity.Keywords;
            news.NewsId = entity.NewsId;
            news.Sequence = entity.Sequence;
            news.Source = entity.Source;
            news.Title = entity.Title;


            if (isNew)
                _dataBase.Archive.Add(news);

            _dataBase.SaveChanges();

            result = news.NewsId;


            return result;
        }
        #endregion

        #region 图片
        /// <summary>
        /// 设置封面
        /// </summary>
        public int SetCover(int imageId)
        {
            int result = 0;

            var album = _dataBase.Album.FirstOrDefault(i => i.ImageId == imageId);

            if (album != null)
            {
                _dataBase.Album.AsEnumerable().Where(i => i.NewsId == album.NewsId).Select(i =>
                {
                    i.IsCover = false;
                    return false;
                }).ToList();
                album.IsCover = true;
                _dataBase.SaveChanges();
            }

            return result;

        }

        public List<EAlbum> DeleteAlbum(string[] ids)
        {
            var albumList = _dataBase.Album.AsEnumerable().Join(ids
                , a => a.ImageId, b => Convert.ToInt32(b)
                , (a, b) => { return a; }).ToList();
            foreach (var album in albumList)
            {
                _dataBase.Album.Remove(album);
            }
            _dataBase.SaveChanges();

            return albumList;
        }

        public void UpdateAlbum(int imageId, string title, string desc, int sequence)
        {

            var album = _dataBase.Album.FirstOrDefault(i => i.ImageId == imageId);
            if (album != null)
            {
                album.Title = title;
                album.Description = desc;
                album.Sequence = sequence;
                this.UpdateAlbum(album);
            }

        }
        /// <summary>
        /// 更新
        /// </summary>
        public int UpdateAlbum(EAlbum eAlbum)
        {
            int result = 0;

            var album = _dataBase.Album.FirstOrDefault(i => i.ImageId == eAlbum.ImageId);
            bool isNew = album == null;
            if (isNew)
                album = new EAlbum();

            album.Description = eAlbum.Description;
            album.ImageId = eAlbum.ImageId;
            album.ImageName = eAlbum.ImageName;
            album.IsCover = eAlbum.IsCover;
            album.NewsId = eAlbum.NewsId;
            album.Sequence = eAlbum.Sequence;
            album.Title = eAlbum.Title;
            if (isNew)
                _dataBase.Album.Add(album);

            result = _dataBase.SaveChanges();

            return result;

        }

        #endregion
    }
}
